<?php

namespace ctrl;

use libs\view;

class cache extends base
{

	/*
	缓存管理
	*/
	public static function index()
	{
		$DirSize = array();
		/**前台静态缓存 */
		$DirSize['htmlCahe'] = FileSizeFormat(DirSize(P_TMP . 'html/'));
		$htmlCahe = DirFileSize(P_TMP . 'html/'); //计算文件及目录数量
		$DirSize['fileHtmlCaheNum'] = $htmlCahe['fileNum'] ?? 0;
		$DirSize['dirHtmlCaheNum'] = $htmlCahe['dirNum'] ?? 0;
		/**数据库文件缓存 */
		$DirSize['fileCahe'] = FileSizeFormat(DirSize(P_CACHE));
		$fileCacheNum = DirFileSize(P_CACHE);
		$DirSize['fileCaheNum'] = $fileCacheNum['fileNum'] ?? 0;
		$DirSize['dirCaheNum'] = $fileCacheNum['dirNum'] ?? 0;
		/**前台程序缓存 */
		$DirSize['fileHomeCaheDir'] = P_TMP . 'run/index/';
		$fileHomeCaheDir = DirFileSize($DirSize['fileHomeCaheDir']); //计算文件及目录数量
		$DirSize['fileHomeCaheNum'] = $fileHomeCaheDir['fileNum'] ?? 0;
		$DirSize['dirHomeCaheNum'] = $fileHomeCaheDir['dirNum'] ?? 0;
		$DirSize['fileHomeCahe'] = FileSizeFormat(DirSize($DirSize['fileHomeCaheDir']));
		/**后台程序缓存 */
		$DirSize['fileAdminCaheDir'] = P_TMP . 'run/admin/';
		$fileAdminCaheDir = DirFileSize($DirSize['fileAdminCaheDir']); //计算文件及目录数量
		$DirSize['fileAdminCaheNum'] = $fileAdminCaheDir['fileNum'] ?? 0;
		$DirSize['dirAdminCaheNum'] = $fileAdminCaheDir['dirNum'] ?? 0;
		$DirSize['fileAdminCahe'] = FileSizeFormat(DirSize($DirSize['fileAdminCaheDir']));
		/**日志 */
		$DirSize['warningLogDir'] = P_TMP . 'warning_log/';
		$warningLogDir = DirFileSize($DirSize['warningLogDir']); //计算文件及目录数量
		$DirSize['fileWarningLogNum'] = $warningLogDir['fileNum'] ?? 0;
		$DirSize['dirWarningLogNum'] = $warningLogDir['dirNum'] ?? 0;
		$DirSize['warningLog'] = FileSizeFormat(DirSize($DirSize['warningLogDir']));

		view::assign('DirSize', $DirSize);
		view::display();
	}


	//清除文件缓存
	static function delFileCache()
	{
		switch ($_GET['id']) {
			case 1:
				//DelDir(P_CACHE, 1);
				$pdo = \libs\pdo::Init();
				$pdo->CleanCache($GLOBALS['ZPHP_CONFIG']['DB']['db']);
				SqlLog("清除 数据库缓存");

				Response(array('status' => 1, 'info' => '清除 数据库缓存 成功'));
				break;
			case 2:
				DelDir(P_TMP . 'run/admin/', 1);
				SqlLog("清除 后台程序缓存");

				Response(array('status' => 1, 'info' => '清除 后台程序缓存 成功'));
				break;
			case 3:
				DelDir(P_TMP . 'html/', 1);
				SqlLog("清除 HTML缓存");

				Response(array('status' => 1, 'info' => '清除 HTML缓存 成功'));
				break;
			case 4:
				DelDir(P_TMP . 'run/index/', 1);
				SqlLog("清除 前台程序缓存");

				Response(array('status' => 1, 'info' => '清除 前台程序缓存 成功'));
				break;
			case 5:
				DelDir(P_TMP . 'warning_log/', 1);
				SqlLog("清除 系统日志");

				Response(array('status' => 1, 'info' => '清除 系统日志 成功'));
				break;
			case 6:
				DelFile("index.html");
				SqlLog("删除 前台首页 index.html 文件");

				Response(array('status' => 1, 'info' => '清除 前台首页 index.html 文件 成功'));
				break;
		}
	}
}
